extern "C"{
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
}

#include "blog_home_page.h"
#include "lib_markdown_parse.h"
#include <fstream>
#include <sstream>
#include <utility>

string strim_string(string before){
    before = before.erase(0,before.find_first_not_of(" "));
    before = before.erase(before.find_last_not_of(" ")+1);
    return before;
}


bool is_md_file(string name){
    name = strim_string(name);
    size_t pos = name.find_last_of(".");
    string md = string("md");

    if(strcasecmp( name.substr(pos+1,string::npos).c_str(),md.c_str()) == 0){
        return true;
    }

    return false;
}

bool create_dir(string dir_name){
    //file do not exist ,return true
    if(access(dir_name.c_str(),F_OK) == 0){
        return true;
    }
    //0755 is a normal mkdir directory right
    if(mkdir(dir_name.c_str(),0755) == 0){
        return true;
    }
    cout << "error:create dir " << dir_name << endl;
    return false;
}


blog_home_page::blog_home_page(){

}

blog_home_page::~blog_home_page(){

}

bool blog_home_page::clear_all(){
    //clear string
    html_header.clear();
    html_header.shrink_to_fit();

    html_left_sidebar.clear();
    html_left_sidebar.shrink_to_fit();

    html_item.clear();
    html_item.shrink_to_fit();

    html_pagination.clear();
    html_pagination.shrink_to_fit();

    html_md_contents.clear();
    html_md_contents.shrink_to_fit();

    html_md_catalog.clear();
    html_md_catalog.shrink_to_fit();

    html_md_header.clear();
    html_md_header.shrink_to_fit();

    html_right_sidebar.clear();
    html_right_sidebar.shrink_to_fit();

    html_ending.clear();
    html_ending.shrink_to_fit();
    //end clear string



    //clear vector<string>
    blog_files.clear();
    blog_files.shrink_to_fit();

    blog_files_without_dir.clear();
    blog_files_without_dir.shrink_to_fit();

    blog_files_subdir.clear();
    blog_files_subdir.shrink_to_fit();

    html_index_page_v.clear();
    html_index_page_v.shrink_to_fit();

    html_index_contents_v.clear();
    html_index_contents_v.shrink_to_fit();
    //end clear vector

    //clear map and multimap
    html_archive_contents_vv.clear();
    html_tags_contents_vv.clear();
    html_archive_page_vv.clear();
    html_tags_page_vv.clear();

    tags_mmap_blog_file_attr.clear();
    month_secs_mmap_blog_file_attr.clear();
    date_secs_mmap_blog_file_attr.clear();
    //end clear map and multimap

    return true;
}

bool blog_home_page::get_blog_dir(string blog_dir){
    DIR *dir;
    struct dirent *ptr;

    dir = opendir(blog_dir.c_str());
    if(dir == NULL){
        cout << "open dir " << blog_dir << " error\n";
        return false;
    }


    while((ptr = readdir(dir)) != NULL){
        if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0){
            continue;
        }
        else if((ptr->d_type == DT_REG) && (is_md_file(ptr->d_name))){
            blog_files.push_back(blog_dir + ptr->d_name);
            blog_files_without_dir.push_back(ptr->d_name);
        } //file
        else if(ptr->d_type == DT_LNK){
            continue;
        }//link file
        else if(ptr->d_type == DT_DIR){
            blog_files_subdir.push_back(ptr->d_name);
            continue;
        }//dir
    }

    closedir(dir);

    return true;
}

bool blog_home_page::get_blog_subdir(string blog_dir){
    DIR *dir;
    struct dirent *ptr;

    dir = opendir(blog_dir.c_str());
    if(dir == NULL){
        cout << "open dir " << blog_dir << " error\n";
        return false;
    }

    vector<string> empty_vec_1;
    empty_vec_1.push_back(string(""));

    blog_files_subdir.clear();
    blog_files_subdir.swap(empty_vec_1);


    while((ptr = readdir(dir)) != NULL){
        if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0){
            continue;
        }
        else if((ptr->d_type == DT_REG)){
            blog_files.push_back(blog_dir + ptr->d_name);
            blog_files_without_dir.push_back(ptr->d_name);
        } //file
        else if(ptr->d_type == DT_LNK){
            continue;
        }//link file
        else if(ptr->d_type == DT_DIR){
            blog_files_subdir.push_back(ptr->d_name);
            continue;
        }//dir
    }

    closedir(dir);

    return true;
}

bool blog_home_page::get_blog_files_attr(){
    //bfi means blog file iterator
    vector<string>::iterator bfi;
    vector<string>::iterator bfi_without_dir;
    blog_file_attr_t bfa;
    size_t pos;
    string tag;
    string after_tags;

    //dbfavt means date_secs_mmap_blog_file_attr value type
    typedef multimap<long,blog_file_attr_t,greater<long>>::value_type dbfavt;
    //mbfavt means month_secs_mmap_blog_file_attr value type
    typedef multimap<long,blog_file_attr_t,greater<long>>::value_type mbfavt;
    //tbfavt means tags_mmap_blog_file_attr value type
    typedef multimap<string,blog_file_attr_t,less<string>>::value_type tbfavt;

    bfi_without_dir = blog_files_without_dir.begin();
    for(bfi = blog_files.begin();
        bfi != blog_files.end();bfi++){

        if(get_a_blog_file_attr(&bfa,(*bfi))){
            bfa.blog_file_time.blog_file_name = *bfi_without_dir;
            date_secs_mmap_blog_file_attr.insert(dbfavt(bfa.date_secs,bfa));
            month_secs_mmap_blog_file_attr.insert(mbfavt(bfa.month_secs,bfa));

            tag = bfa.tags;
            pos = tag.find_first_of(" ");
            if(pos == string::npos){
                tags_mmap_blog_file_attr.insert(tbfavt(tag,bfa));
            }
            else{
                after_tags = tag.substr(0,pos);
                tags_mmap_blog_file_attr.insert(tbfavt(strim_string(after_tags),bfa));
                after_tags = tag.substr(pos+1,string::npos);
                do{
                    pos = after_tags.find_first_of(" ");
                    if(pos == string::npos){
                        tags_mmap_blog_file_attr.insert(tbfavt(strim_string(after_tags),bfa));
                    }
                    else{
                        tag = after_tags.substr(0,pos);
                        tags_mmap_blog_file_attr.insert(tbfavt(strim_string(tag),bfa));
                        after_tags = after_tags.substr(pos+1,string::npos);
                    }
                }while(pos != string::npos);
            }
        }
        bfi_without_dir++;
    }
    return true;
}


bool blog_home_page::get_a_blog_file_attr(blog_file_attr_t *blog_file_attr, string blog_file){
    ifstream file;
    file.open(blog_file,ios::in);
    if(!file){
        cout << "fail to open the file 1 " << blog_file << endl;
        return false;
    }

    stringstream file_stream;
    file_stream << file.rdbuf();

    string blog_line;
    getline(file_stream,blog_line);
    size_t pos;
    int max_times;
    string temp_str;
    string temp_month_str;
    tm temp_tm;
    if(blog_line == string("<!--")){
        max_times = 0;
        do{
            max_times = max_times + 1;
            getline(file_stream,blog_line);
            if(blog_line.substr(0,6) == string("author")){
                pos = blog_line.find_first_of(":");
                blog_file_attr->author = blog_line.substr(pos+1,string::npos);
            }
            else if(blog_line.substr(0,4) == string("date")){
                pos = blog_line.find_first_of(":");
                temp_str = blog_line.substr(pos+1,string::npos);
                blog_file_attr->date = temp_str ;
                strptime(blog_file_attr->date.c_str(),"%Y-%m-%d %H:%M:%S",&temp_tm);
                temp_tm.tm_isdst = -1;
                blog_file_attr->date_secs = mktime(&temp_tm);

                pos = temp_str.find_last_of("-");
                temp_month_str.clear();
                temp_month_str = temp_str.substr(0,pos) + string("-01 00:00:00");
                strptime(temp_month_str.c_str(),"%Y-%m-%d %H:%M:%S",&temp_tm);
                temp_tm.tm_isdst = -1;
                blog_file_attr->month_secs = mktime(&temp_tm);

                temp_str = temp_str.substr(0,pos);
                temp_str = temp_str.erase(0,temp_str.find_first_not_of(" "));
                temp_str = temp_str.erase(temp_str.find_last_not_of(" ")+1);

                blog_file_attr->month = temp_str.substr(0,pos);
            }
            else if(blog_line.substr(0,5) == string("title")){
                pos = blog_line.find_first_of(":");
                blog_file_attr->title = blog_line.substr(pos+1,string::npos);
            }
            else if(blog_line.substr(0,4) == string("tags")){
                pos = blog_line.find_first_of(":");
                blog_file_attr->tags = blog_line.substr(pos+1,string::npos);
            }
            else if(blog_line.substr(0,7) == string("summary")){
                pos = blog_line.find_first_of((":"));
                blog_file_attr->summary = blog_line.substr(pos+1,string::npos);
            }
            else if(blog_line.substr(0,5) == string("stick")){
                pos = blog_line.find_first_of(":");
                blog_file_attr->stick = blog_line.substr(pos+1,string::npos);
            }
            else if(blog_line == string("-->")){
                break;
            }
        }while(max_times < MAX_TIMES);
    }

    blog_file_attr->blog_file_time.blog_file_name_with_dir = blog_file;

    file.close();
    return true;
}

bool blog_home_page::create_blog_index_page(){
    string html_index_page = string("");
    vector<string>::iterator it;
    for(it = html_index_contents_v.begin();
        it != html_index_contents_v.end();
        it++){
        html_index_page = html_header + string("<div class=\"main-page-box\">\n") +
                (*it) + html_left_sidebar + html_right_sidebar + string("</div>\n")
                + html_ending;
        html_index_page_v.push_back(html_index_page);
        html_index_page.clear();
    }
    return true;
}

bool blog_home_page::create_blog_archive_page(){
    string html_archive_page = string("");
    map<string,string,less<string> >::iterator it;
    typedef map<string,string,less<string> >::value_type hacvt;

    for(it = html_archive_contents_vv.begin();
        it != html_archive_contents_vv.end();
        it++){
        html_archive_page  = html_header + string("<div class=\"main-page-box\">\n") +
                it->second + html_left_sidebar + html_right_sidebar + string("</div>\n")
                + html_ending;

        html_archive_page_vv.insert(hacvt(it->first,html_archive_page));
        html_archive_page.clear();
    }
    return true;
}

bool blog_home_page::create_blog_tags_page(){
    string html_tags_page = string("");
    map<string,string,less<string> >::iterator it;
    typedef map<string,string,less<string> >::value_type htpvt;
    for(it = html_tags_contents_vv.begin();
        it != html_tags_contents_vv.end();
        it++){
        html_tags_page  = html_header + string("<div class=\"main-page-box\">\n") +
                it->second + html_left_sidebar + html_right_sidebar + string("</div>\n")
                + html_ending;
        html_tags_page_vv.insert(htpvt(it->first,html_tags_page));
        html_tags_page.clear();
    }
    return true;
}

bool blog_home_page::create_blog_header(string html_title,
                                        string blog_name,
                                        string blog_subname){
    html_header = string("<!DOCTYPE html>\n"
                         "<html lang=\"zh_CN\">\n"
                         "<head>\n<meta "
                         "charset=\"UTF-8\">"
                         "<meta name=\"viewport\" "
                         "content=\"width=device-width, initial-scale=1\">\n"
                         "\n<title>") +
            html_title +
            string("</title>\n<link rel=\"stylesheet\" "
                   "type=\"text/css\" href=\"style."
                   "css\">\n</head>\n<body>\n"
                   "<div class=\"main-header-box\">\n"
                   "<h1 class=\"main-header\">\n"
                   "<a href=\"/\" rel=\"home\">") +
            blog_name +
            string("</a>\n</h1>\n<h3 class=\"main-header\">")+
            blog_subname +
            string("</h3>\n</div>\n<div "
                   "class=\"main-header-line\"></div>\n"
                   "<div class=\"main-nav-box\" "
                   "id=\"main-nav\">\n<div "
                   "class=\"main-nav\">\n<a href=\"/\" "
                   "rel=\"home\">首页</a>\n</div>\n"
                   "<div class=\"main-nav\">|</div>\n"
                   "<div class=\"main-nav\">\n<a "
                   "href=\"/about_me.html\">关于我</a>\n"
                   "</div>\n</div>\n");
    return true;
}

bool blog_home_page::create_blog_left_sidebar() {
    html_left_sidebar = string("<div class=\"left-sidebar\">\n"
                               "<h3>文章归档(共");
    html_left_sidebar = html_left_sidebar +
            to_string( blog_files.size()) +
            string("篇)</h3>\n");
    multimap<long,blog_file_attr_t,greater<long>>::iterator i;
    for(i = month_secs_mmap_blog_file_attr.begin();
        i != month_secs_mmap_blog_file_attr.end();
        ){
        pair <multimap<long,blog_file_attr_t,greater<long> >::iterator,
                multimap<long,blog_file_attr_t,greater<long> >::iterator>
                ret;

        ret = month_secs_mmap_blog_file_attr.equal_range(i->first);
        html_left_sidebar = html_left_sidebar +
                string("<h4><a href=\"/archive/") +
                ret.first->second.month + string(".html\">") +
                ret.first->second.month + string("(") +
                to_string(month_secs_mmap_blog_file_attr.count(ret.first->first)) +
                string("篇)</a></h4>\n");
        for(i = ret.first;i != ret.second;){
            i++;
        }


    }
    html_left_sidebar = html_left_sidebar + string("</div>\n");

    return true;
}

bool blog_home_page::create_blog_right_sidebar(){
    html_right_sidebar = string("<div class=\"right-sidebar\">"
                                "<h3>标签分类</h3>\n");

    multimap<string,blog_file_attr_t,less<string>>::iterator i;
    for(i = tags_mmap_blog_file_attr.begin();
        i != tags_mmap_blog_file_attr.end();
        ){

        pair<multimap<string,blog_file_attr_t,less<string> >::iterator,
                multimap<string,blog_file_attr_t,less<string> >::iterator>
                ret;
        ret = tags_mmap_blog_file_attr.equal_range(i->first);
        if(!ret.first->first.empty()){
            html_right_sidebar = html_right_sidebar +
                    string("<h4><a href=\"/tags/") +
                    ret.first->first + string(".html\">") +
                    ret.first->first + string("(") +
                    to_string(tags_mmap_blog_file_attr.count(ret.first->first)) +
                    string("篇)</a></h4>\n");
        }
        for(i = ret.first;i != ret.second;i++){

        }

    }

    html_right_sidebar = html_right_sidebar +
            string("</div>\n");

    return true;
}

bool blog_home_page::create_blog_ending(){
    html_ending = string("<div class=\"footer\">\n"
                         "<p>Powered by <a href=\""
                         "https://gitee.com/iamdsy/nginx-blog\" "
                         "target=\"_blank\">nginx-blog</a></p>\n"
                         "</div>\n</body>\n</html>");
    return true;
}

bool blog_home_page::create_blog_index_contents(int max_items_per_page){
    int pages = 0;
    string html_index_contents;
    multimap<long,blog_file_attr_t,greater<long>>::iterator it;

    for(it = date_secs_mmap_blog_file_attr.begin();
        it != date_secs_mmap_blog_file_attr.end();
        ){

        html_index_contents = string("<div class=\"contents\">\n");
        for(int j = 0;j < max_items_per_page;j++){
            if(it != date_secs_mmap_blog_file_attr.end()){
                if(create_blog_item(it->second)){
                    html_index_contents = html_index_contents + html_item;
                }
                html_item.clear();
                it++;
            }
        }

        pages = pages + 1;
        if(create_blog_pagination(pages,string("index"),max_items_per_page)){
            html_index_contents = html_index_contents + html_pagination;
        }

        html_index_contents = html_index_contents + string("</div>\n");
        html_index_contents_v.push_back(html_index_contents);
        html_index_contents.clear();
    }
    return true;
}

bool blog_home_page::create_blog_archive_contents(int max_items_per_page){
    int pages = 0;
    string month;
    string html_archive_contents;
    string page_type = string("/archive/");
    multimap<long,blog_file_attr_t,greater<long>>::iterator it;
    typedef map<string,string,less<string> >::value_type hacvt;

    for(it = month_secs_mmap_blog_file_attr.begin();
        it != month_secs_mmap_blog_file_attr.end();
        ){
        pair <multimap<long,blog_file_attr_t,greater<long> >::iterator,
                multimap<long,blog_file_attr_t,greater<long> >::iterator>
                ret;

        ret = month_secs_mmap_blog_file_attr.equal_range(it->first);

        for(it = ret.first;it != ret.second;){
            html_archive_contents = string("<div class=\"contents\">\n");
            for(int j = 0;j < max_items_per_page;j++){
                if(create_blog_item(it->second)){
                    html_archive_contents = html_archive_contents + html_item;
                }
                html_item.clear();
                it++;
                if((it == month_secs_mmap_blog_file_attr.end())
                        || it == ret.second){
                    break;
                }
            }
            if(pages == 0){
                month =  ret.first->second.month;
            }
            else{
                month =  ret.first->second.month +
                        string("_") + to_string(pages+1);
            }
            pages = pages+1;

            int max_item = month_secs_mmap_blog_file_attr.count(ret.first->first);
            int max_page =  max_item / max_items_per_page;
            if((max_item % max_items_per_page) > 0){
                max_page = max_page + 1;
            }
            if(create_archive_tags_blog_pagination(pages,string("/archive/"),
                                                   max_page,ret.first->second.month)){
                html_archive_contents = html_archive_contents + html_pagination;
            }


            html_archive_contents = html_archive_contents + string("</div>\n");
            html_archive_contents_vv.insert(hacvt(month,html_archive_contents));

            if((max_page == pages) && (max_page > 1)){
                month =  ret.first->second.month +
                        string("_last");
                html_archive_contents_vv.insert(hacvt(month,html_archive_contents));
            }

            month.clear();
            html_archive_contents.clear();

            if(it == month_secs_mmap_blog_file_attr.end()){
                break;
            }
        }
        pages = 0;
    }
    return true;
}

bool blog_home_page::create_blog_tags_contents(int max_items_per_page){
    int pages = 0;
    string html_tags_contents;
    string tags;
    string page_type = string("/tags/");
    multimap<string,blog_file_attr_t,less<string> >::iterator it;
    typedef map<string,string,less<string> >::value_type hacvt;

    for(it = tags_mmap_blog_file_attr.begin();
        it != tags_mmap_blog_file_attr.end();
        ){
        pair<multimap<string,blog_file_attr_t,less<string> >::iterator,
                multimap<string,blog_file_attr_t,less<string> >::iterator>
                ret;

        ret = tags_mmap_blog_file_attr.equal_range(it->first);
        for(it = ret.first;it != ret.second;){
            html_tags_contents = string("<div class=\"contents\">\n");
            for(int j = 0;j < max_items_per_page;j++){
                if(create_blog_item(it->second)){
                    html_tags_contents = html_tags_contents + html_item;
                }
                html_item.clear();
                it++;
                if((it == tags_mmap_blog_file_attr.end())
                        || (it == ret.second)){
                    break;
                }

            }
            //for some reason do not clearly
            if(!ret.first->first.empty()){
                if(pages == 0){
                    tags =  ret.first->first;
                }
                else{
                    tags =  ret.first->first +
                            string("_") + to_string(pages+1);
                }
            }
            pages = pages+1;

            int max_item =tags_mmap_blog_file_attr.count(ret.first->first);
            int max_page =  max_item / max_items_per_page;
            if((max_item % max_items_per_page) > 0){
                max_page = max_page + 1;
            }
            if(create_archive_tags_blog_pagination(pages,string("/tags/"),
                                                   max_page,ret.first->first)){
                html_tags_contents = html_tags_contents + html_pagination;
            }

            html_tags_contents = html_tags_contents + string("</div>\n");
            html_tags_contents_vv.insert(hacvt(tags,html_tags_contents));
            if((max_page == pages) && (max_page > 1) && (!ret.first->first.empty())){
                tags =  ret.first->first +
                        string("_last");
                html_tags_contents_vv.insert(hacvt(tags,html_tags_contents));
            }
            tags.clear();
            html_tags_contents.clear();
            if(it == tags_mmap_blog_file_attr.end()){
                break;
            }
        }
        pages = 0;
    }
    return true;
}


bool blog_home_page::create_blog_item(blog_file_attr_t bfa){
    html_item = string("<div class=\"contents-item\">\n<h1>");
    html_item = html_item + bfa.title + string("</h1>");
    html_item = html_item + string("</div>\n<div "
                                   "class=\"contents-item\">\n"
                                   "<div class=\"time"
                                   "-tags-box\">\n<div class=\"time\">");
    html_item = html_item + bfa.date + string(" / ");
    html_item = html_item + bfa.author + string("</div>");

    size_t pos;

    if(!bfa.tags.empty()){
        string tag;
        string after_tag;
        html_item = html_item + string("<div class=\"tags\">标签：\n");
        pos = bfa.tags.find_first_of(" ");
        if(pos == string::npos){
            html_item = html_item + string("<span>\n") +
                    string("<a href=\"/tags/") +
                    bfa.tags + string(".html\" rel = \"tag\">") +
                    bfa.tags + string("</a>\n</span>\n");
        }
        else{
            tag = bfa.tags.substr(0,pos);
            after_tag = bfa.tags.substr(pos+1,string::npos);
            html_item = html_item + string("<span>\n") +
                    string("<a href=\"/tags/") +
                    tag + string(".html\" rel = \"tag\">") +
                    tag + string("</a>\n</span>\n");

            do{
                pos = after_tag.find_first_of(" ");
                if(pos == string::npos){
                    html_item = html_item + string("<span>\n") +
                            string("<a href=\"/tags/") +
                            after_tag + string(".html\" rel = \"tag\">") +
                            after_tag + string("</a>\n</span>\n");
                }
                else{
                    tag = after_tag.substr(0,pos);
                    after_tag = after_tag.substr(pos+1,string::npos);
                    html_item = html_item + string("<span>\n") +
                            string("<a href=\"/tags/") +
                            tag + string(".html\" rel = \"tag\">") +
                            tag + string("</a>\n</span>\n");
                }
            }while(pos != string::npos);
        }
        html_item = html_item + string("</div>\n");
    }

    html_item = html_item + string("</div>\n</div>\n");

    html_item = html_item + string("<div class=\"contents-item summary\">\n");
    html_item = html_item + bfa.summary + string("</div>\n");

    html_item = html_item + string("<div class=\"contents-item\">\n");
    html_item = html_item + string("<a href=\"/blog/");

    pos = bfa.blog_file_time.blog_file_name.find_last_of(".");
    if(pos == string::npos){
        html_item = html_item + bfa.blog_file_time.blog_file_name;
    }
    else{
        html_item = html_item + bfa.blog_file_time.blog_file_name.substr(0,pos);
    }
    html_item = html_item + string(".html\">阅读全文</a>\n</div>");

    return true;
}


bool blog_home_page::create_blog_pagination(int pages, string page_type,
                                            int max_item_per_page){
    int max_page = blog_files.size() / max_item_per_page;
    if((blog_files.size() % max_item_per_page) > 0 ){
        max_page = max_page + 1;
    }

    if(max_page == 1){
        html_pagination = string("");
        return true;
    }

    html_pagination = string("<div class=\"contents-item\">\n"
                             "<h4 class=\"pagination\">\n"
                             "<span class=\"pagination\"><a href=\"");
    html_pagination = html_pagination + page_type +
            string(".html\"><<</a></span>\n");
    if(max_page - pages >= 5){
        for (int i = pages;i <= pages+5;i++){
            if(i == pages){
                if(i == 1){
                    html_pagination = html_pagination +
                            string("<span class=\"pagination active\"><a href=\"")+
                            page_type  + string(".html\">") +
                            to_string(i) + string("</a></span>\n");
                }
                else{
                    html_pagination = html_pagination +
                            string("<span class=\"pagination active\"><a href=\"")+
                            page_type + string("_") + to_string(i) + string(".html\">") +
                            to_string(i) + string("</a></span>\n");
                }
            }
            else{
                if(i == 1){
                    html_pagination = html_pagination +
                            string("<span class=\"pagination\"><a href=\"")+
                            page_type  + string(".html\">") +
                            to_string(i) + string("</a></span>\n");
                }
                else{
                    html_pagination = html_pagination +
                            string("<span class=\"pagination\"><a href=\"")+
                            page_type + string("_") + to_string(i) + string(".html\">") +
                            to_string(i) + string("</a></span>\n");
                }
            }
        }
    }
    else{
        int i;
        if(max_page >= 7){
            i = max_page - 5;
        }
        else{
            i = 1;
        }
        for (;i <= max_page;i++){
            if(i == pages){
                if(i == 1){
                    html_pagination = html_pagination +
                            string("<span class=\"pagination active\"><a href=\"")+
                            page_type  + string(".html\">") +
                            to_string(i) + string("</a></span>\n");
                }
                else{
                    html_pagination = html_pagination +
                            string("<span class=\"pagination active\"><a href=\"")+
                            page_type + string("_") + to_string(i) + string(".html\">") +
                            to_string(i) + string("</a></span>\n");
                }
            }
            else{
                if(i == 1){
                    html_pagination = html_pagination +
                            string("<span class=\"pagination\"><a href=\"")+
                            page_type  + string(".html\">") +
                            to_string(i) + string("</a></span>\n");
                }
                else{
                    html_pagination = html_pagination +
                            string("<span class=\"pagination\"><a href=\"")+
                            page_type + string("_") + to_string(i) + string(".html\">") +
                            to_string(i) + string("</a></span>\n");
                }
            }
        }

    }


    html_pagination = html_pagination +
            string("<span class=\"pagination\"><a href=\"")+
            page_type + string("_last.html\">>></a></span>\n");

    html_pagination = html_pagination + string("</h4>\n</div>\n");
    return true;
}

bool blog_home_page::create_archive_tags_blog_pagination(int pages, string page_type,
                                                         int max_pages,string arch_tags){
    if(max_pages == 1){
        html_pagination = string("");
        return true;
    }
    html_pagination = string("<div class=\"contents-item\">\n"
                             "<h4 class=\"pagination\">\n"
                             "<span class=\"pagination\"><a href=\"");
    html_pagination = html_pagination + page_type +
            arch_tags + string(".html\"><<</a></span>\n");

    if(max_pages - pages >= 5){
        for (int i = pages;i <= pages+5;i++){
            if(i == pages){
                if(i == 1){
                    html_pagination = html_pagination +
                            string("<span class=\"pagination active\"><a href=\"")+
                            page_type  + arch_tags + string(".html\">") +
                            to_string(i) + string("</a></span>\n");
                }
                else{
                    html_pagination = html_pagination +
                            string("<span class=\"pagination active\"><a href=\"")+
                            page_type + arch_tags + string("_") + to_string(i) +
                            string(".html\">") + to_string(i) + string("</a></span>\n");
                }
            }
            else{
                if(i == 1){
                    html_pagination = html_pagination +
                            string("<span class=\"pagination active\"><a href=\"")+
                            page_type  + arch_tags + string(".html\">") +
                            to_string(i) + string("</a></span>\n");
                }
                else{
                    html_pagination = html_pagination +
                            string("<span class=\"pagination\"><a href=\"")+
                            page_type + arch_tags + string("_") + to_string(i)
                            + string(".html\">") + to_string(i) + string("</a></span>\n");
                }
            }
        }
    }
    else{
        int i;
        if(max_pages >= 7){
            i = max_pages - 5;
        }
        else{
            i = 1;
        }
        for (;i <= max_pages;i++){
            if(i == pages){
                if(i == 1){
                    html_pagination = html_pagination +
                            string("<span class=\"pagination active\"><a href=\"")+
                            page_type  + arch_tags + string(".html\">") +
                            to_string(i) + string("</a></span>\n");
                }
                else{
                    html_pagination = html_pagination +
                            string("<span class=\"pagination active\"><a href=\"")+
                            page_type + arch_tags + string("_") + to_string(i) +
                            string(".html\">") + to_string(i) + string("</a></span>\n");
                }
            }
            else{
                if(i == 1){
                    html_pagination = html_pagination +
                            string("<span class=\"pagination\"><a href=\"")+
                            page_type  + arch_tags + string(".html\">") +
                            to_string(i) + string("</a></span>\n");
                }
                else{
                    html_pagination = html_pagination +
                            string("<span class=\"pagination\"><a href=\"")+
                            page_type + arch_tags + string("_") + to_string(i)
                            + string(".html\">") + to_string(i) + string("</a></span>\n");
                }
            }
        }

    }


    html_pagination = html_pagination +
            string("<span class=\"pagination\"><a href=\"")+
            page_type + arch_tags + string("_last.html\">>></a></span>\n");

    html_pagination = html_pagination + string("</h4>\n</div>\n");
    return true;
}

bool blog_home_page::create_md_header(string html_title,
                                      string blog_name,
                                      string blog_subname,
                                      blog_file_attr_t bfa){

    html_md_header = string("<!DOCTYPE html>\n"
                            "<html lang=\"zh_CN\">\n"
                            "<head>\n<meta "
                            "charset=\"UTF-8\">"
                            "<meta name=\"viewport\" "
                            "content=\"width=device-width, initial-scale=1\">\n"
                            "\n<title>") + bfa.title + string(" | ") +
            html_title +
            string("</title>\n"
                   "<meta name=\"keyword\" contents=\"") +
            bfa.tags +
            string("\">\n<meta name=\"description\" contents\"") +
            bfa.summary +
            string("\">\n<link rel=\"stylesheet\" "
                   "type=\"text/css\" href=\"style."
                   "css\">\n</head>\n<body>\n"
                   "<div class=\"main-header-box\">\n"
                   "<h1 class=\"main-header\">\n"
                   "<a href=\"/\" rel=\"home\">") +
            blog_name +
            string("</a>\n</h1>\n<h3 class=\"main-header\">")+
            blog_subname +
            string("</h3>\n</div>\n<div "
                   "class=\"main-header-line\"></div>\n"
                   "<div class=\"main-nav-box\" "
                   "id=\"main-nav\">\n<div "
                   "class=\"main-nav\">\n<a href=\"/\" "
                   "rel=\"home\">首页</a>\n</div>\n"
                   "<div class=\"main-nav\">|</div>\n"
                   "<div class=\"main-nav\">\n<a "
                   "href=\"/about_me.html\">关于我</a>\n"
                   "</div>\n</div>\n");
    return true;
}


bool blog_home_page::create_md_contens(blog_file_attr_t bfa){
    string about_me = string("about_me.md");

    if(strcasecmp(about_me.c_str(),bfa.blog_file_time.blog_file_name.c_str()) != 0){

    html_md_contents = string("<div class=\"contents\">\n"
                              "<div class=\"contents-item\">\n"
                              "<h1>\n<a href=\"/blog/");
    //get blog html file name
    string temp_str = bfa.blog_file_time.blog_file_name;
    int dot = temp_str.find_last_of(".");
    temp_str = temp_str.substr(0,dot) + string(".html\">");

    html_md_contents = html_md_contents + temp_str +
            bfa.title + string("</a>\n");
    //end html file name

    }
    else{
    html_md_contents = string("<div class=\"contents\">\n");
    //get blog html file name
    }



    if(strcasecmp(about_me.c_str(),bfa.blog_file_time.blog_file_name.c_str()) != 0){
        //get blog time author
        html_md_contents = html_md_contents + string("</h1>\n</div>\n<div class=\"contents-item\">\n"
                                                     "<div class=\"time-tags-box\">\n<div class=\"time\">");
        html_md_contents = html_md_contents + bfa.date + string(" / ") +
                bfa.author + string(" </div>");
        //end time author

        size_t pos;

        //get blog tags
        string html_md_tag;
        if(!bfa.tags.empty()){
            string tag;
            string after_tag;
            html_md_tag = html_md_tag + string("<div class=\"tags\">标签：\n");
            pos = bfa.tags.find_first_of(" ");
            if(pos == string::npos){
                html_md_tag = html_md_tag + string("<span>\n") +
                        string("<a href=\"/tags/") +
                        bfa.tags + string(".html\" rel = \"tag\">") +
                        bfa.tags + string("</a>\n</span>\n");
            }
            else{
                tag = bfa.tags.substr(0,pos);
                after_tag = bfa.tags.substr(pos+1,string::npos);
                html_md_tag = html_md_tag + string("<span>\n") +
                        string("<a href=\"/tags/") +
                        tag + string(".html\" rel = \"tag\">") +
                        tag + string("</a>\n</span>\n");

                do{
                    pos = after_tag.find_first_of(" ");
                    if(pos == string::npos){
                        html_md_tag = html_md_tag + string("<span>\n") +
                                string("<a href=\"/tags/") +
                                after_tag + string(".html\" rel = \"tag\">") +
                                after_tag + string("</a>\n</span>\n");
                    }
                    else{
                        tag = after_tag.substr(0,pos);
                        after_tag = after_tag.substr(pos+1,string::npos);
                        html_md_tag = html_md_tag + string("<span>\n") +
                                string("<a href=\"/tags/") +
                                tag + string(".html\" rel = \"tag\">") +
                                tag + string("</a>\n</span>\n");
                    }
                }while(pos != string::npos);
            }
            html_md_tag = html_md_tag + string("</div>\n");
        }

        html_md_contents = html_md_contents + html_md_tag;
        html_md_contents = html_md_contents + string("</div>\n</div>\n");
        //end blog tags
    }



    //get blog contents

    ifstream file;
    file.open(bfa.blog_file_time.blog_file_name_with_dir,
              ios::in);//open the input file
    if(!file){
        cout << "fail to open the file 2 " << bfa.blog_file_time.blog_file_name_with_dir
             << endl;
        return false;
    }

    stringstream str_stream;
    str_stream << file.rdbuf();//read the file
    markdown_parse hehe;
    bool ret = hehe.parse_file(&str_stream);
    if(ret == false){
        cout << "some thing wrong happened\n";
        file.close();
        return false;
    }
    file.close();


    html_md_contents = html_md_contents + string("<div class=\"contents-item\">\n");
    html_md_contents = html_md_contents + hehe.parsed_all_string + string("</div>\n</div>\n");

    if(hehe.catalog.empty()){
        html_md_catalog =  string("<div class=\"left-sidebar\">\n") +
                string("<h3> </h3>\n");
        html_md_catalog = html_md_catalog+ hehe.catalog + string("</div>");
    }
    else{
        if(strcasecmp(about_me.c_str(),bfa.blog_file_time.blog_file_name.c_str()) != 0){
            html_md_catalog =  string("<div class=\"left-sidebar\">\n") +
                    string("<h3>目录</h3>\n");
            html_md_catalog = html_md_catalog+ hehe.catalog + string("</div>");
	}
	else{
            html_md_catalog =  string("<div class=\"left-sidebar\">\n") +
                    string("<h3> </h3>\n");
            html_md_catalog = html_md_catalog+ hehe.catalog + string("</div>");
	}
    }

    return true;

}


























































